import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import pandas as pd

# 打开商品详情页
browser = webdriver.Chrome()
browser.maximize_window()
browser.get('https://item.jd.com/7344084.html')

# 等待用户登录
time.sleep(30)

# 单击“商品评价”按钮
comment_button = browser.find_element(By.CSS_SELECTOR, 'li[data-anchor="#comment"]')
comment_button.click()
time.sleep(10)

# 单击“只看当前商品评价”复选框
current_sku_checkbox = browser.find_element(By.CSS_SELECTOR, 'input#comm-curr-sku')
current_sku_checkbox.click()

# 爬取前15页的评价
max_page = 15
all_comments = []
for page in range(1, max_page + 1):
    print(f'正在爬取第{page}页...')
    time.sleep(10)
    # 用BeautifulSoup解析网页源代码
    soup = BeautifulSoup(browser.page_source, 'lxml')
    # 提取评价数据并进行清洗
    comments = soup.select('div#comment-0 > div.comment-item')
    for comment in comments:
        # 提取评价等级
        star_tag = comment.select('div.comment-star')
        star = int(star_tag[0].get('class')[-1][-1])
        # 提取评价内容
        content_tags = comment.select('p.comment-con')
        for j in content_tags:
            for k in j.select('br'):
                k.replace_with('\n')
        contents = [i.get_text().strip() for i in content_tags]
        contents = '\n'.join(contents)
        all_comments.append([contents, star])
    
    # 单击“下一页”按钮
    if page < max_page:
        next_button = browser.find_element(By.CSS_SELECTOR, 'div.com-table-footer a.ui-pager-next')
        next_button.click()

# 关闭浏览器
browser.quit()

# 整理和导出数据
df = pd.DataFrame(all_comments, columns=['评价内容', '评价等级'])
df.to_csv('京东商品评价.csv', index=False, encoding='utf-8-sig')
